const Base = require("../base.js");
var axios = require("axios");
module.exports = class extends Base {
  async __before() {
    try {
      this.xcxip = this.ctx.header["x-real-ip"] || "127.0.0.1";
    } catch (e) {
      this.xcxip = "127.0.0.1";
    }
    this.roleId = 98; //角色id
    this.sxname = "wxinfo";
    const dxglobalSetting = this.model("dxglobalSetting");
    const { text } = await dxglobalSetting
      .where({ roleId: this.roleId, sxname: this.sxname })
      .find();
    const { AppID, AppSecret } = JSON.parse(text);
    this.access_token = await this.cache("gzhaccess_tokenmaya6");
    this.access_token = ""
    this.gzhAppID = AppID;
    this.gzhAppSecret = AppSecret;
    // if (!this.access_token) {
    //   const res = await this.dxGet("https://api.weixin.qq.com/cgi-bin/token", {
    //     grant_type: "client_credential",
    //     appid: this.gzhAppID,
    //     secret: this.gzhAppSecret,
    //   });
    //   this.access_token = res.access_token;
    //   await this.cache("gzhaccess_tokenmaya6", res.access_token, {
    //     timeout: 1000 * 60 * 60 * 1,
    //   });
    // }
    this.ticket = await this.cache("ticket");
    if (!this.ticket) {
      const { ticket } = await this.dxGet(
        "https://api.weixin.qq.com/cgi-bin/ticket/getticket?access_token=" +
          this.access_token +
          "&type=jsapi"
      );
      this.ticket = ticket;
      await this.cache("ticket", ticket, {
        timeout: 1000 * 60 * 60 * 1,
      });
    }
  }
  // 生成推广二维码
  async getcreateQrAction() {
    const res = await this.dxPost(
      "https://api.weixin.qq.com/cgi-bin/qrcode/create?access_token=" +
        this.access_token,
      {
        expire_seconds: "2592000",
        action_name: "QR_STR_SCENE",
      }
    );
    this.success({
      ...res,
      access_token:this.access_token,
      qrUrl: "https://mp.weixin.qq.com/cgi-bin/showqrcode?ticket=" + res.ticket,
    });
  }

  async  thzxcfAction(){
    const res = await this.dxPost(
      "https://api.weixin.qq.com/cgi-bin/message/template/send?access_token=" +
        this.access_token,
      {
        touser: "oN1fJ5w1GGnyCi5ooajTZCPPumOQ",
        template_id: "ztSJNP6KCmAulNnnXK_C_22zURT1a2tSwXNBD--pg6Y",
        data: {
          first: {
            value: "111111",
            color: "#173177",
          },
          keyword1: {
            value: "no.1234",
            color: "#173177",
          },
          keyword2: {
            value: "2024-07-28 18:10",
            color: "#173177",
          },
          remark: {
            value: "备注",
            color: "#173177",
          },
        },
      }
    );
    this.success({
      openid:"oN1fJ5w1GGnyCi5ooajTZCPPumOQ",
      ...res
    });
  }
};
